package com.tjakus.parser;

import java.io.StringReader;
import java.util.*;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import com.tjakus.hibernate.Advertisement;
import com.tjakus.logging.Logging;

/**
 * This class parses XML content and pulls out advertisement data
 * 
 * @author tjakus
 * 
 */
public class XMLManager {

	private static String commonUrl = "http://www.njuskalo.hr";

	/**
	 * This method parses XML data and returns the advertisement data.
	 * 
	 */
	public static Advertisement getAdvertisementData(String str) {

		Advertisement advertisement = new Advertisement();
		Logging.getLogger().info("XMLManager - getAdvertisementData [ PARSE XML ]");
		try {
			// initialize SAX builder
			SAXBuilder builder = new SAXBuilder();
			Document doc = builder.build(new StringReader(str));
			Element rootNode = doc.getRootElement();
			for (Element x : rootNode.getChildren()) {
				// if child name is "a" then there is a title and URL
				if (x.getName().equalsIgnoreCase("a")) {
					Logging.getLogger().debug(
							"XMLManager - getAdvertisementData - title [ " + x.getText()
									+ " ]");
					Logging.getLogger().debug(
							"XMLManager - getAdvertisementData - url [ "
									+ x.getAttributeValue("href") + " ]");
					advertisement.setTitle(x.getText());
					advertisement.setUrl(commonUrl + x.getAttributeValue("href"));
				// Get price from XML if njuskalo normal URL is used (check njuskalo-normal.txt)
				} else if (x.getName().equalsIgnoreCase("span")
						&& x.getAttributeValue("class").equalsIgnoreCase("price")) {
					List<Element> z = x.getChildren();
					Logging.getLogger().debug(
							"XMLManager - getAdvertisementData - price [ "
									+ z.get(0).getText() + " ]");
					advertisement.setPrice(z.get(0).getText());
				} else {
					for (Element y : x.getChildren()) {
						if (y.getName().equalsIgnoreCase("span")
								&& y.getAttributeValue("class").equalsIgnoreCase("abstract")) {
							// replace all white spaces (trim) and all tabs
							advertisement.setDescription(y.getText().trim()
									.replaceAll("\t", ""));
							Logging.getLogger().debug(
									"XMLManager - getAdvertisementData - description [ "
											+ advertisement.getDescription() + " ]");
						} else if (y.getName().equalsIgnoreCase("span")
								&& y.getAttributeValue("class").equalsIgnoreCase("date")) {
							advertisement.setDate(y.getText().split(": ")[1]);
							Logging.getLogger().debug(
									"XMLManager - getAdvertisementData - date [ "
											+ advertisement.getDate() + " ]");
						// Get price from XML if njuskalo URL search is used (check njuskalo-serach.txt)
						} else if (y.getName().equalsIgnoreCase("span")
								&& y.getAttributeValue("class").equalsIgnoreCase("price")) {
							List<Element> z = y.getChildren();
							Logging.getLogger().debug(
									"XMLManager - getAdvertisementData - price [ "
											+ z.get(0).getText() + " ]");
							advertisement.setPrice(z.get(0).getText());
						}
					}
				}
			}
			return advertisement;
		} catch (Exception e) {
			Logging.getLogger().error(
					"XMLManager - getAdvertisementData - [ " + str + " ]", e);
			return null;
		}
	}
}
